<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$return['basic']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在！请重新预约','error');
	}
	$order=pdo_get('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
	$serve=pdo_get('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']));
	$serve_title=$serve['title'];
	$serve_times=$serve['serve_time'];
	$serve_times=json_decode($serve_times,true);
	$serve_time=$serve_times[$order['timeid']];
	$package=json_decode($order['package'],true);
	$user=pdo_get('jy_gohome_user',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
//	$serve_time=json_decode($order['serve_time']);
	$this->session('order_id',$order_id);
	$post=array();
	$post['name']=$user['name'];
	$post['mobile']=$user['mobile'];
	$post['package_price']=$package['package_price'];
	$post['jifen']=$serve['jifen'];
	$return['serve_title']=$serve_title;
	$return['package']=$package;
	$return['serve_time']=$serve_time;
	$return['day']=$order['day'];
	$return['order']=$order;
	$return['post']=$post;
	exi($return);
}
if($_RQ['op']=='uporder'){
	$post=get_postdata();
	if(!$post){
		exi('信息错误，请重新提交','error');
	}
	$order_id=$this->session('order_id');
	$data=array('name'=>$post['name'],'mobile'=>$post['mobile'],'address'=>$post['address']);
	$data2=array('username'=>$post['name'],'mobile'=>$post['mobile'],'address'=>$post['address']);
	pdo_update('jy_gohome_user',$data,array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$order=pdo_get('jy_gohome_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
	$package=json_decode($order['package'],true);
	pdo_update('jy_gohome_order',array('status'=>'1','pay'=>0),array('id'=>$order['id']));
	$serve=pdo_get('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']));
	$user=pdo_get('jy_gohome_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	$worker=pdo_get('jy_gohome_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['worker_id']));
	$serve_times=pdo_getcolumn('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']),'serve_time');
	$serve_times=json_decode($serve_times,true);
	$serve_time=$serve_times[$order['timeid']];
	$postdata = array(
					'first' => array(
						'value' => '您好，您有新的'.$serve['title'].'预约',
						'color' => '#990000'
					),
					'keyword1' => array(
						'value' => $order['username'],
						'color' => '#ff510'
					),
					'keyword2' => array(
						'value' => $order['day'].'--'.$serve_time['times']['start'].'-'.$serve_time['times']['end'],
						'color' => '#ff510'
					),
					'keyword3' => array(
						'value' => $order['address'],
						'color' => '#ff510'
					),
					'remark' => array(
						'value' => '请尽快受理，谢谢！',
						'color' => '#ff510'
					),							
	);
	$tpl=$this->plugin_setting('message_tpl');
	$url=custom_url('jy_gohome/worker_order/list/status=1&id='.$order['id'],true);
	if($worker['mid'] && $tpl['notice']){
		cfc('account')->sendTplNotice($worker['mid'],$tpl['notice'],$postdata,$url,'#980000');
	}
	if($order['jifen']>0){
		model('core/member')->add_credit1($order['mid'],$order['jifen'],'预约服务送积分');
	}
	exi('预约成功！','success','jy_gohome/order/list/status=1&id='.$order['id']);
}
//支付
if($_RQ['op']=='notice'){
	$post=get_postdata();
	if(!$post){
		exi('信息错误，请重新提交','error');
	}
	$data=array('name'=>$post['name'],'mobile'=>$post['mobile'],'address'=>$post['address']);
	$data2=array('username'=>$post['name'],'mobile'=>$post['mobile'],'address'=>$post['address']);
	//pdo_update('jy_gohome_user',$data,array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
//	pdo_update('jy_gohome_order',$data2,array('id'=>$this->session('order_id'),'uniacid'=>$_SESSION['uniacid']));
	$params['fee']=$post['package_price'];
	$params['title']='预约订单';
	$params['id']=$this->session('order_id');
	$params['plugin']='jy_gohome';
	$params['back_url']='jy_gohome/book/payresult';
	$return['payid']=cfc('pay')->create_pay_order($params);
	exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_gohome_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	$package=json_decode($order['package'],true);
	$order['price']=$package['package_price'];
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_gohome_order',array('status'=>'1','pay'=>$payresult['fee']),array('id'=>$order['id']));
	$order['money']=$order['price'];
	$this->model('commission')->jy_teamfee($order,'预约服务');
	$this->model('commission')->jy_area_agent($order,'预约服务');
	$this->model('commission')->jy_commission($order,'预约服务');
	$serve=pdo_get('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']));
	$user=pdo_get('jy_gohome_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	$worker=pdo_get('jy_gohome_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['worker_id']));
	$serve_times=pdo_getcolumn('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']),'serve_time');
	$serve_times=json_decode($serve_times,true);
	$serve_time=$serve_times[$order['timeid']];
	$postdata = array(
					'first' => array(
						'value' => '您好，您有新的'.$serve['title'].'预约',
						'color' => '#990000'
					),
					'keyword1' => array(
						'value' => $order['username'],
						'color' => '#ff510'
					),
					'keyword2' => array(
						'value' => $order['day'].'--'.$serve_time['times']['start'].'-'.$serve_time['times']['end'],
						'color' => '#ff510'
					),
					'keyword3' => array(
						'value' => $order['address'],
						'color' => '#ff510'
					),
					'remark' => array(
						'value' => '请尽快受理，谢谢！',
						'color' => '#ff510'
					),							
	);
	$tpl=$this->plugin_setting('message_tpl');
	$url=custom_url('jy_gohome/worker_order/list/status=1&id='.$order['id'],true);
	if($order['jifen']>0){
		model('core/member')->add_credit1($order['user_id'],$order['jifen'],'预约服务送积分');
	}
	if($worker['mid'] && $tpl['notice']){
		cfc('account')->sendTplNotice($worker['mid'],$tpl['notice'],$postdata,$url,'#980000');
	}
	exi('支付成功','success','jy_gohome/order/list/status=1&id='.$order['id']);
}